package com.yougou.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yougou.data.model.Address;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class AddressManager {
    
    private static final String KEY_ADDRESSES_PREFIX = "key_addresses_";
    private static List<Address> addresses;
    private static Gson gson = new Gson();
    
    
    private static String getAddressesKey() {
        String uid = UserManager.isLogin() ? UserManager.getCurrentUser().getUid() : "guest";
        return KEY_ADDRESSES_PREFIX + uid;
    }
    
    
    public static void addAddress(Address address) {
        List<Address> addressList = getAddresses();
        
        // 如果是第一个地址或设置为默认，更新默认状态
        if (addressList.isEmpty() || address.isDefault()) {
            // 取消其他地址的默认状态
            for (Address addr : addressList) {
                addr.setDefault(false);
            }
            address.setDefault(true);
        }
        
        address.setId(addressList.size() + 1);
        addressList.add(address);
        saveAddresses(addressList);
    }
    
    
    public static void updateAddress(Address address) {
        List<Address> addressList = getAddresses();
        for (int i = 0; i < addressList.size(); i++) {
            if (addressList.get(i).getId() == address.getId()) {
                // 如果设为默认，取消其他地址的默认状态
                if (address.isDefault()) {
                    for (Address addr : addressList) {
                        addr.setDefault(false);
                    }
                }
                addressList.set(i, address);
                break;
            }
        }
        saveAddresses(addressList);
    }
    
    
    public static void deleteAddress(int id) {
        List<Address> addressList = getAddresses();
        addressList.removeIf(address -> address.getId() == id);
        
        // 如果删除的是默认地址且还有其他地址，设置第一个为默认
        if (!addressList.isEmpty()) {
            boolean hasDefault = false;
            for (Address addr : addressList) {
                if (addr.isDefault()) {
                    hasDefault = true;
                    break;
                }
            }
            if (!hasDefault) {
                addressList.get(0).setDefault(true);
            }
        }
        
        saveAddresses(addressList);
    }
    
    
    public static List<Address> getAddresses() {
        // 每次都重新加载，确保数据是当前用户的
        String json = SPUtils.getString(getAddressesKey());
        if (json.isEmpty()) {
            addresses = new ArrayList<>();
        } else {
            Type type = new TypeToken<List<Address>>() {}.getType();
            addresses = gson.fromJson(json, type);
            if (addresses == null) {
                addresses = new ArrayList<>();
            }
        }
        return addresses;
    }
    
    
    public static Address getDefaultAddress() {
        List<Address> addressList = getAddresses();
        for (Address address : addressList) {
            if (address.isDefault()) {
                return address;
            }
        }
        return addressList.isEmpty() ? null : addressList.get(0);
    }
    
    
    public static void clearAddresses() {
        addresses = new ArrayList<>();
        saveAddresses(addresses);
    }
    
    
    private static void saveAddresses(List<Address> addressList) {
        addresses = addressList;
        String json = gson.toJson(addressList);
        SPUtils.putString(getAddressesKey(), json);
    }
}

